# -*- coding: utf-8 -*-
"""
@author: luhx
@describe: 数据库相关操作
"""
from kds_util.mysql_database import PyMySql
from kds_util.user_logbook import user_log as logger

"""策略操作"""
_create_table_strategy_opt_position_if_not_exists_template = """
  CREATE TABLE IF NOT EXISTS `tbl_strategy_opt_position` (
    `trade_day` int(11) DEFAULT 0 COMMENT '交易日',
    `record_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '记录时间',
    `strategy_type` varchar(12) DEFAULT '' COMMENT '操作类型，open,reopen,close,change',
    `opt_type` varchar(12) DEFAULT '' COMMENT '操作类型，open,close,change',
    `contract` varchar(12) DEFAULT '' COMMENT '合约',
    `breed_side` int(11) NOT NULL DEFAULT 0 COMMENT '1为多，-1为空',
    `change_position` int(11) DEFAULT 0 COMMENT '变动的手数，大于0为增仓，小于0为减仓',
    `old_position` int(11) DEFAULT 0 COMMENT '原有持仓量',
    `margin` decimal(18,3) DEFAULT 0 COMMENT '保证金',
    `profit` decimal(18,3) DEFAULT 0 COMMENT '盈亏',
    `opt_status` int(11) DEFAULT 0 COMMENT '操作状态，针对仓位的操作，成功为1， 不成功为0',
    `remark` varchar(12) DEFAULT '' COMMENT '备注',
    `update_time` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
    PRIMARY KEY (`record_time`, `contract`),
    INDEX  index_tbl_strategy_opt_position_01  (trade_day)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  """


def create_strategy_high_low_table(mysql: PyMySql):
    sql = _create_table_strategy_opt_position_if_not_exists_template
    flag = mysql.exec(sql)
    logger.info(f"创建策略操作表:tbl_strategy_opt_position: {flag}: {sql}")
